home *** CD-ROM | disk | FTP | other *** search
- /* Application Scroller, version 1.0 (August, 26, 1988)
-
- This application demonstrate features and use of the
- Scrolling manager
-
- © 1988 John A. Nairn, All Rights Reserved */
-
- /* Includes from Lightspeed C */
- #include <QuickDraw.h>
- #include <MacTypes.h>
- #include <FontMgr.h>
- #include <WindowMgr.h>
- #include <MenuMgr.h>
- #include <TextEdit.h>
- #include <EventMgr.h>
- #include <DeskMgr.h>
- #include <DialogMgr.h>
- #include <ToolboxUtil.h>
- #include <ControlMgr.h>
- #include "ScrollMgr.h" /*Scrolling Manager header*/
-
- enum { apple_menu=255,file_menu,edit_menu };
- enum { drawCursor=300,handCursor,joyCursor };
- #define sample_text 500
- #define about_dlog 998
- #define NIL 0L /* Some constants */
- #define RULERWIDTH 15
- #define TOOLWIDTH 20
- #define about_item 1 /* Menu items */
- enum { new_window=1,graphic_window=3,text_edit_window,
- vert_bar=6,horiz_bar,vert_ruler=9,horiz_ruler,
- vert_tool_bar,horiz_tool_bar,quit=14 };
-
- WindowPtr myWindow=NIL; /* Global Variables */
- TEHandle myhTE=NIL;
- ScrollInfo scrollRecord;
- ScrollPtr myScrlp;
- Point polygon[200];
- Rect dragrect;
- int window_type=graphic_window,has_vBar=TRUE;
- int has_vRuler=FALSE,has_vTools=FALSE,has_hRuler=FALSE;
- int has_hTools=FALSE,has_hBar=TRUE;
- int vRuler,vTools,hRuler,hTools,polySize=0;
-
- /* Main Program - Initialize and start event loop */
-
- main()
- {
- MenuHandle menu;
- Rect r;
-
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitCursor();
- FlushEvents(everyEvent,0);
- InitMenus();
- TEInit();
- InitDialogs(0L);
- r=screenBits.bounds; /* Window dragging Rect */
- SetRect(&dragrect,4,24,r.right-4,r.bottom-4);
- menu=GetMenu(apple_menu); /*DA menu*/
- AddResMenu(menu,'DRVR');
- InsertMenu(menu,0);
- menu=GetMenu(file_menu); /*File menu*/
- InsertMenu(menu,0);
- CheckItem(menu,window_type,TRUE);
- CheckItem(menu,horiz_bar,has_vBar);
- CheckItem(menu,vert_bar,has_hBar);
- InsertMenu(GetMenu(edit_menu),0); /*Edit Menu*/
- MaintainMenus();
- do_about();
- event_loop();
- }
-
- /* Enable or Disable Edit Menu */
-
- MaintainMenus()
- {
- if(FrontWindow()==myWindow)
- { if(myhTE==NIL)
- DisableItem(GetMHandle(edit_menu),0);
- else
- EnableItem(GetMHandle(edit_menu),0);
- }
- else
- EnableItem(GetMHandle(edit_menu),0);
- DrawMenuBar();
- }
-
- /* Maintain cursor according to window type, keys
- down, and mouse location */
-
- MaintainCursor()
- {
- KeyMap theKeys;
- RgnHandle drawRgn;
- Point where;
-
- if(FrontWindow()!=myWindow)
- return;
- else if(myWindow==NIL)
- InitCursor();
- else
- { drawRgn=ScrollSectRgn(myScrlp,myWindow);
- GetMouse(&where);
- if(!PtInRgn(where,drawRgn))
- InitCursor();
- else
- { GetKeys(&theKeys);
- if((theKeys.Key[1]&4)&&(theKeys.Key[1]&1))
- SetCursor(*GetCursor(joyCursor));
- else if(theKeys.Key[1]&4)
- SetCursor(*GetCursor(handCursor));
- else if(myhTE!=NIL)
- SetCursor(*GetCursor(iBeamCursor));
- else
- SetCursor(*GetCursor(drawCursor));
- }
- DisposeRgn(drawRgn);
- }
- }
-
- /* Program main event loop */
-
- event_loop()
- {
- EventRecord event;
- Boolean valid;
- char theChar;
-
- while (1)
- { SystemTask();
- MaintainCursor();
- if(myhTE!=NIL) TEIdle(myhTE);
- valid=GetNextEvent(everyEvent,&event);
- if (!valid) continue;
- switch(event.what)
- { case mouseDown:
- do_mouse_down(&event);
- break;
- case keyDown:
- case autoKey:
- theChar=event.message&charCodeMask;
- if(event.modifiers&cmdKey)
- { if(event.what!=autoKey)
- do_menu(MenuKey(theChar));
- }
- else
- { if(myhTE!=NIL)
- { if((**myhTE).teLength<32760)
- { TEKey(theChar,myhTE);
- if(myScrlp->vScrollHdl!=NIL)
- SetScroll(myScrlp,
- (**myhTE).nLines,VertBar);
- FixTEInsertPt();
- }
- else
- SysBeep(10); /* TE full */
- }
- else
- SysBeep(10); /* bad key down */
- }
- break;
- case updateEvt:
- do_update(&event);
- break;
- case activateEvt:
- do_activate((WindowPtr)event.message,
- event.modifiers&activeFlag);
- break;
- default:
- break;
- }
- }
- }
-
- /* Handle mouse down events */
-
- do_mouse_down(eventp)
- EventRecord *eventp;
- {
- WindowPtr windowp;
- int place_type=FindWindow(eventp->where,&windowp);
- Boolean theshift;
- KeyMap theKeys;
-
- switch(place_type)
- { case inMenuBar:
- do_menu(MenuSelect(eventp->where));
- break;
- case inSysWindow:
- SystemClick(eventp,windowp);
- MaintainMenus();
- break;
- case inContent:
- if(windowp!=FrontWindow())
- { SelectWindow(windowp);
- MaintainMenus();
- }
- else if(windowp==myWindow)
- { if(DoScroll(windowp,eventp->where)==FALSE)
- { GetKeys(&theKeys);
- if((theKeys.Key[1]&4)&&
- (theKeys.Key[1]&1))
- JoyStickScroll(eventp->where);
- else if(theKeys.Key[1]&4)
- DragScroll(eventp->where);
- else if(myhTE==NIL)
- TrackPoly(eventp->where);
- else
- { GlobalToLocal(&eventp->where);
- if(PtInRect(eventp->where,
- &(**myhTE).viewRect))
- { if(eventp->modifiers&shiftKey)
- theshift=TRUE;
- else
- theshift=FALSE;
- ScrollTEClick(eventp->where,
- theshift,myhTE);
- }
- else
- SysBeep(10);
- }
- }
- }
- break;
- case inDrag:
- DragWindow(windowp,eventp->where,&dragrect);
- break;
- case inGrow:
- grow_window(windowp,eventp->where);
- break;
- case inGoAway:
- if(TrackGoAway(windowp,eventp->where))
- { do_close(windowp);
- MaintainMenus();
- }
- break;
- default:
- break;
- }
- }
-
- /* Track mouse and draw a polygon - for Graphic windows*/
-
- TrackPoly(where)
- Point where;
- {
- int lm,tm;
- Point lastWhere;
- Rect drawRect;
- RgnHandle drawRgn;
-
- GlobalToLocal(&where);
- ScrollSectRect(myScrlp,myWindow,&drawRect);
- if(!PtInRect(where,&drawRect))
- { SysBeep(10); /* illegal click location */
- return;
- }
- GetMargins(myScrlp,&lm,inPixels,&tm,inPixels);
- if(polySize==0)
- { polygon[polySize].h=where.h+lm;
- polygon[polySize].v=where.v+tm;
- }
- else if(polySize==200)
- { SysBeep(10);
- return;
- }
- drawRgn=ScrollSectRgn(myScrlp,myWindow);
- SetClip(drawRgn);
- PenMode(patXor);
- MoveTo(polygon[polySize].h-lm,polygon[polySize].v-tm);
- LineTo(where.h,where.v);
- lastWhere=where;
- while(StillDown())
- { GetMouse(&where);
- if(PtInRgn(where,drawRgn))
- { if((where.h!=lastWhere.h)||
- (where.v!=lastWhere.v))
- { MoveTo(polygon[polySize].h-lm,
- polygon[polySize].v-tm);
- LineTo(lastWhere.h,lastWhere.v);
- MoveTo(polygon[polySize].h-lm,
- polygon[polySize].v-tm);
- LineTo(where.h,where.v);
- lastWhere=where;
- }
- }
- else
- { MoveTo(polygon[polySize].h-lm,
- polygon[polySize].v-tm);
- LineTo(lastWhere.h,lastWhere.v);
- FollowMouse();
- GetMargins(myScrlp,&lm,inPixels,&tm,inPixels);
- MoveTo(polygon[polySize].h-lm,
- polygon[polySize].v-tm);
- LineTo(where.h,where.v);
- lastWhere=where;
- }
- }
- MoveTo(polygon[polySize].h-lm,polygon[polySize].v-tm);
- LineTo(lastWhere.h,lastWhere.v);
- PenMode(patCopy);
- if(lastWhere.h>drawRect.right)
- lastWhere.h=drawRect.right;
- if(lastWhere.h<drawRect.left)
- lastWhere.h=drawRect.left;
- if(lastWhere.v>drawRect.bottom)
- lastWhere.v=drawRect.bottom;
- if(lastWhere.v<drawRect.top)
- lastWhere.v=drawRect.top;
- MoveTo(polygon[polySize].h-lm,polygon[polySize].v-tm);
- LineTo(lastWhere.h,lastWhere.v);
- polygon[++polySize].h=lastWhere.h+lm;
- polygon[polySize].v=lastWhere.v+tm;
- ClipRect(&myWindow->portRect);
- DisposeRgn(drawRgn);
- }
-
- /* Close the scrolling demonstration window */
-
- do_close(windowp)
- WindowPtr windowp;
- {
- if (myWindow!=NIL)
- { if(myhTE!=NIL)
- { ZeroScrap();
- TEToScrap();
- TEDispose(myhTE);
- }
- CloseWindow(windowp);
- myWindow=NIL;
- myhTE=NIL;
- }
- }
-
- /* Handle menu command events */
-
- do_menu(command)
- long command;
- {
- int menu_id=HiWord(command);
- int item=LoWord(command);
- if(menu_id!=0)
- do_menu_item(menu_id,item);
- }
-
- /* Do a menu command */
-
- do_menu_item(menu_id,item)
- int menu_id,item;
- {
- char item_name[32];
- int newCheck;
-
- switch(menu_id)
- { case apple_menu:
- switch(item)
- { case about_item:
- do_about();
- break;
- default:
- GetItem(GetMHandle(menu_id),
- item,item_name);
- OpenDeskAcc(item_name);
- HiliteMenu(0);
- MaintainMenus();
- break;
- }
- break;
- case file_menu:
- switch (item)
- { case new_window:
- do_close(myWindow);
- myNewWindow();
- HiliteMenu(0);
- MaintainMenus();
- break;
- case graphic_window:
- case text_edit_window:
- CheckItem(GetMHandle(file_menu),
- window_type,FALSE);
- window_type=item;
- CheckItem(GetMHandle(file_menu),
- window_type,TRUE);
- break;
- case vert_bar:
- newCheck=has_vBar=1-has_vBar;
- break;
- case horiz_bar:
- newCheck=has_hBar=1-has_hBar;
- break;
- case vert_ruler:
- newCheck=has_vRuler=1-has_vRuler;
- break;
- case horiz_ruler:
- newCheck=has_hRuler=1-has_hRuler;
- break;
- case vert_tool_bar:
- newCheck=has_vTools=1-has_vTools;
- break;
- case horiz_tool_bar:
- newCheck=has_hTools=1-has_hTools;
- break;
- case quit:
- do_close(myWindow);
- finish();
- default:
- break;
- }
- if(item>=vert_bar)
- CheckItem(GetMHandle(file_menu),
- item,newCheck);
- break;
- case edit_menu:
- if(!SystemEdit(item-1))
- { switch(item-1)
- { case undoCmd:
- break;
- case cutCmd:
- TECut(myhTE);
- break;
- case copyCmd:
- TECopy(myhTE);
- break;
- case pasteCmd:
- if(((long)(**myhTE).teLength+
- (long)TEGetScrapLen())<32760)
- TEPaste(myhTE);
- else
- SysBeep(10);
- break;
- case clearCmd:
- TEDelete(myhTE);
- break;
- default:
- break;
- }
- /* reset scroll bar */
- if(myScrlp->vScrollHdl!=NIL)
- SetScroll(myScrlp,(**myhTE).nLines,
- VertBar);
-
- /* if needed, move insertion point */
- if(((item-1)==cutCmd)||
- ((item-1)==clearCmd)||
- ((item-1)==pasteCmd))
- FixTEInsertPt();
- }
- default:
- break;
- }
- HiliteMenu(0);
- }
-
- /* Handle Update events */
-
- do_update(event)
- EventRecord *event;
- {
- WindowPtr up_wind=(WindowPtr)event->message;
-
- UpdateWindow(up_wind,FALSE);
- }
-
- /* Update the window */
-
- UpdateWindow(up_wind,scrollType)
- WindowPtr up_wind;
- int scrollType;
- {
- GrafPtr save_graf;
-
- if(up_wind==myWindow)
- { GetPort(&save_graf);
- SetPort(up_wind);
- BeginUpdate(up_wind);
- ClipRect(&up_wind->portRect);
- EraseRect(&up_wind->portRect);
- if(!scrollType)
- { DrawControls(up_wind);
- ScrollDrawGrowIcon(myScrlp,up_wind);
- }
- draw_content(up_wind,scrollType);
- EndUpdate(up_wind);
- SetPort(save_graf);
- }
- }
-
- /* Activate the window */
-
- do_activate(act_wind,isactivate)
- WindowPtr act_wind;
- int isactivate;
- {
- Rect r;
-
- SetPort(act_wind);
- if(act_wind==myWindow)
- { ScrollDrawGrowIcon(myScrlp,act_wind);
- if(isactivate)
- { ScrollActivate(myScrlp);
- if(myhTE!=NIL)
- { TEActivate(myhTE);
- TEFromScrap();
- }
- }
- else
- { ScrollDeactivate(myScrlp);
- if(myhTE!=NIL)
- { TEDeactivate(myhTE);
- ZeroScrap();
- TEToScrap();
- }
- }
- }
- }
-
- /* handle window growing */
-
- grow_window(windowp,mouse_point)
- WindowPtr windowp;
- Point mouse_point;
- {
- long new_bounds;
-
- InvalBars(windowp); /*invalidate scroll bars*/
- if((new_bounds=GrowWindow(windowp,mouse_point,
- &dragrect))==0)
- return;
- SizeWindow(windowp,LoWord(new_bounds),
- HiWord(new_bounds),(Boolean)TRUE);
- InvalBars(windowp); /*invalidate new scroll bars*/
- GrowScroll(windowp); /*update scroll bar window*/
- }
-
- /* Open new scrolling example window */
-
- myNewWindow()
- {
- Rect r,wrect;
- int hLineSize=0,vLineSize=0,vTotalLines=0;
- int vToolSize=0,vRulerSize=0,hToolSize=0,hRulerSize=0;
- int vScrollTop=0,hScrollLeft=0,hTotalLines=0;
- Boolean includeText=FALSE;
- FontInfo theFont;
- Handle texthdl;
-
- r=screenBits.bounds;
- SetRect(&wrect,20,60,r.right-20,r.bottom-20);
- myWindow=NewWindow(NIL,&wrect,"\pScrolling Example",
- 1,documentProc,-1L,1,0L);
-
- polySize=0;
- SetPort(myWindow);
- TextFont(geneva);
- TextSize(12);
- /*Note: add 1 to ruler and tool widths for line*/
- if(has_vRuler) vRulerSize=RULERWIDTH+1;
- if(has_hRuler) hRulerSize=RULERWIDTH+1;
- if(has_vTools)
- { vToolSize=TOOLWIDTH+1;
- hScrollLeft=TOOLWIDTH+1;
- }
- if(has_hTools)
- { hToolSize=TOOLWIDTH+1;
- vScrollTop=TOOLWIDTH+1;
- }
- if(has_hBar) hLineSize=18;
- if(window_type==graphic_window)
- { vTotalLines=hTotalLines=40;
- if(has_vBar) vLineSize=18;
- }
- else
- { if(has_vBar)
- { GetFontInfo(&theFont);
- vLineSize=theFont.ascent+theFont.descent+
- theFont.leading;
- }
- vTotalLines=0;
- if(has_hBar)
- hTotalLines=(wrect.right-wrect.left-vToolSize-
- vRulerSize-SBarWidth)/hLineSize;
- includeText=TRUE;
- }
-
- /* Seto up scoll bars */
- myScrlp=SetScrollWindow(myWindow,&scrollRecord,
- includeText,vLineSize,vTotalLines,vToolSize,
- vRulerSize,vScrollTop,hLineSize,hTotalLines,
- hToolSize,hRulerSize,hScrollLeft,NIL);
-
- /* If text window, add some sample text */
- myhTE=myScrlp->hTE;
- if(myhTE!=NIL)
- { texthdl=GetResource('GNRL',sample_text);
- HLock(texthdl);
- TESetText(*texthdl,SizeResource(texthdl),myhTE);
- HUnlock(texthdl);
- ReleaseResource(texthdl);
- if(myScrlp->vScrollHdl!=NIL)
- SetScroll(myScrlp,(**myhTE).nLines,VertBar);
- }
-
- /* Set current window tool and ruler flags */
- vTools=has_vTools;
- hTools=has_hTools;
- vRuler=has_vRuler;
- hRuler=has_hRuler;
- }
-
- /* Draw the window contents */
-
- draw_content(windowp,scrollType)
- WindowPtr windowp;
- int scrollType;
- {
- register int i;
- int lineSize=20,lm,tm,tick,nexttick;
- int vtoolsize=0,htoolsize=0,xtick,ytick,vtotal,htotal;
- static char vertTools[11]={10,'V','E','R','T',' ','T',
- 'O','O','L','S'};
- static char horizTools[12]={11,'H','O','R','I','Z',
- ' ','T','O','O','L','S'};
- static int length[8]={0,5,8,5,11,5,8,5};
- char nstr[20];
- Rect drawRect,tempRect;
- RgnHandle drawRgn;
-
- GetMargins(myScrlp,&lm,inPixels,&tm,inPixels);
- ClipRect(NonScrollRect(myScrlp,windowp,&drawRect));
- if(vTools) vtoolsize=TOOLWIDTH;
- if(hTools) htoolsize=TOOLWIDTH;
-
- if(vTools&&(!scrollType)) /*vert tool bar*/
- { MoveTo(vtoolsize,drawRect.top);
- LineTo(vtoolsize,drawRect.bottom);
- TextFace(outline+bold+shadow);
- for(i=1;i<=vertTools[0];i++)
- { MoveTo(0,htoolsize+20*i);
- LineTo(vtoolsize,htoolsize+20*i);
- MoveTo(6,htoolsize+20*i-5);
- DrawChar(vertTools[i]);
- }
- TextFace(0);
- }
-
- if(hTools&&(!scrollType)) /*horiz tool bar*/
- { MoveTo(drawRect.left,htoolsize);
- LineTo(drawRect.right,htoolsize);
- TextFace(outline+bold+shadow);
- for(i=1;i<=horizTools[0];i++)
- { MoveTo(vtoolsize+25*i,0);
- LineTo(vtoolsize+25*i,htoolsize);
- MoveTo(vtoolsize+25*i-17,15);
- DrawChar(horizTools[i]);
- }
- TextFace(0);
- }
-
- if(vTools&&hTools&&(!scrollType))
- markX(0,0,htoolsize,vtoolsize);
-
- if(vRuler&&hRuler&&(!scrollType))
- markX(vtoolsize,htoolsize,RULERWIDTH,RULERWIDTH);
-
- TextSize(9);
- if(vRuler&&(scrollType!=HorizBar))
- { vtotal=vtoolsize+RULERWIDTH;
- MoveTo(vtotal,drawRect.top+htoolsize);
- LineTo(vtotal,drawRect.bottom);
- drawRect.top=htoolsize+RULERWIDTH*hRuler;
- ClipRect(&drawRect);
- tick=tm/72;
- nexttick=drawRect.top+72*tick-tm-72;
- while((nexttick+=72)<=drawRect.bottom)
- { MoveTo(vtoolsize,nexttick);
- LineTo(vtotal,nexttick);
- if(tick)
- { NumToString((long)tick,nstr);
- MoveTo(vtotal-StringWidth(nstr),
- nexttick+8);
- DrawString(nstr);
- }
- for(i=1;i<=7;i++)
- { MoveTo(vtotal,ytick=nexttick+9*i);
- LineTo(vtotal-length[i],ytick);
- }
- tick++;
- }
- }
-
- if(hRuler&&(scrollType!=VertBar))
- { htotal=htoolsize+RULERWIDTH;
- drawRect.top=0;
- ClipRect(&drawRect);
- MoveTo(drawRect.left+vtoolsize,htotal);
- LineTo(drawRect.right,htotal);
- drawRect.left=vtoolsize+RULERWIDTH*vRuler;
- ClipRect(&drawRect);
- tick=lm/72;
- nexttick=drawRect.left+72*tick-lm-72;
- while((nexttick+=72)<=drawRect.right+8)
- { MoveTo(nexttick,htoolsize);
- LineTo(nexttick,htotal);
- if(tick)
- { NumToString((long)tick,nstr);
- MoveTo(nexttick-1-StringWidth(nstr),
- htoolsize+10);
- DrawString(nstr);
- }
- for(i=1;i<=7;i++)
- { MoveTo(xtick=nexttick+9*i,htotal);
- LineTo(xtick,htotal-length[i]);
- }
- tick++;
- }
- }
- TextSize(12);
-
- if(myhTE==NIL) /*Graphic Window Update*/
- { drawRgn=ScrollSectRgn(myScrlp,windowp);
- SetClip(drawRgn);
- MoveTo(polygon[0].h-lm,polygon[0].v-tm);
- for(i=1;i<=polySize;i++)
- LineTo(polygon[i].h-lm,polygon[i].v-tm);
- ClipRect(&windowp->portRect);
- DisposeRgn(drawRgn);
- }
- else /*Text Window Update*/
- { ClipRect(&windowp->portRect);
- tempRect=(**myhTE).viewRect;
- TEUpdate(&tempRect,myhTE);
- }
- }
-
- /* Draw X in window */
-
- markX(beginx,beginy,xwidth,ywidth)
- int beginx,beginy,xwidth,ywidth;
- { MoveTo(beginx,beginy);
- LineTo(beginx+xwidth,beginy+ywidth);
- MoveTo(beginx,beginy+ywidth);
- LineTo(beginx+xwidth,beginy);
- }
-
- /* Present about box and wait for mouse click */
-
- do_about()
- {
- DialogPtr aboutBox;
- EventRecord event;
-
- aboutBox=GetNewDialog(about_dlog,NIL,-1L);
- DrawDialog(aboutBox);
-
- /* wait for key or mouse click -
- from MacTutor, V4,#3,pg10 */
- while(!EventAvail(keyDownMask|autoKeyMask|mDownMask,
- &event))
- SystemTask();
-
- if(event.what==mouseDown) /* remove mouse down */
- { GlobalToLocal(&event.where);
- if(PtInRect(event.where,&aboutBox->portRect))
- GetNextEvent(mDownMask,&event);
- }
-
- DisposDialog(aboutBox);
- }
-
- /* Exit to shell after saving TE scrap */
-
- finish()
- {
- ZeroScrap();
- TEToScrap();
- LoadScrap();
- ExitToShell();
- }
-